/**
 * <p>Title: ServiceZMCEC1.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-23 下午04:46:08  张志芳  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.ce.EquipmentClass;
import com.baosight.sczx.common.zm.cInterface.ce.StandardEquipment;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCE;
import com.baosight.sczx.zm.ce.domain.ZMCEEquipmentCard;
import com.baosight.sczx.zm.ce.domain.ZMCELubricationStandard;
import com.baosight.sczx.zm.ce.domain.ZMCEMaintenanceStandard;
import com.baosight.sczx.zm.ce.domain.ZMCEPointStandard;
import com.baosight.sczx.zm.ce.domain.ZMCERepairStandard;

/**
 * 设备卡片
 * <p>Title: ServiceZMCEC1.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-23 下午04:46:08  张志芳  Create
 *</pre>
 */
public class ServiceZMCEC1 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEEquipmentCard</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-8上午09:57:14 张志芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(EquipmentClass.getEquipmentClassBlock("cmes.ce.equipmentClass", null));//设备分类
		outInfo.addBlock(Engineering.getEngineering("cmes.cp.engineering", null, true));//工程
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", true));//部门
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.ce.equipmentSource", true));//设备来源
		return outInfo;
	}

	/**
	 * 设备查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEEquipmentCard</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:36 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdToencode = inInfo.getString("fdToencode");
		StaticTools.setQueryParameters(inInfo, "fdToencode", fdToencode, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCEEquipmentCard.query", new ZMCEEquipmentCard());

		return outInfo;
	}
	
	/**
	 * 复制设备卡片
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE12</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-18下午04:59:22 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo copy(EiInfo inInfo) {
		for(int i=0; i<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			//根据基础设置中的是否关联分类编号来产生设备编号
			if(SystemSettingCE.getInstance().getIsIncludeClass())
			{
				String str = ConnectStr(inInfo.getBlock("result").getCell(i, "fdToencode" ).toString(),inInfo.getBlock("result").getCell(i, "fdSuffix" ).toString());//拼接传参
				inInfo.getBlock("result").setCell(i, "fdCode", str.substring(2));//赋值
			}
			else
			{
				inInfo.getBlock("result").setCell(i, "fdCode", inInfo.getBlock("result").getCell(i, "fdSuffix" ));
			}
			Map params = new HashMap();
			params.put("fdCode", inInfo.getBlock("result").getCell(i, "fdCode" ));
			List result = dao.query("ZMCEEquipmentCard.validate", params);
			if(result!=null && result.size() > 0) {
				inInfo.setStatus(-1);
				inInfo.setMsg(inInfo.getBlock("result").getCell(i, "fdCode" )+"编号已经存在！");
				return inInfo;
			}
			inInfo.getBlock("result").setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_EQUIPMENTCARD"));//主键
		}
		EiInfo outinfo = super.insert(inInfo, "ZMCEEquipmentCard.insert", "result");
		return outinfo;
	}
	
	/**
	 * 设备新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEEquipmentCard</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:46 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//根据基础设置中的是否关联分类编号来产生设备编号
		if(SystemSettingCE.getInstance().getIsIncludeClass())
		{
			String str = ConnectStr(inInfo.getBlock("result").getCell(0, "fdToencode" ).toString(),inInfo.getBlock("result").getCell(0, "fdSuffix" ).toString());//拼接传参
			inInfo.getBlock("result").setCell(0, "fdCode", str.substring(2));//赋值
		}
		else
		{
			inInfo.getBlock("result").setCell(0, "fdCode", inInfo.getBlock("result").getCell(0, "fdSuffix" ));
		}
		
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCEEquipmentCard.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {		
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_EQUIPMENTCARD"));//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCEEquipmentCard.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * 设备修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEEquipmentCard</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:23:56 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {		
		//根据基础设置中的是否关联分类编号来产生设备编号
		if(SystemSettingCE.getInstance().getIsIncludeClass())
		{
			String str = ConnectStr(inInfo.getBlock("result").getCell(0, "fdToencode" ).toString(),inInfo.getBlock("result").getCell(0, "fdSuffix" ).toString());//拼接传参
			inInfo.getBlock("result").setCell(0, "fdCode", str.substring(2));//赋值
		}
		else
		{
			inInfo.getBlock("result").setCell(0, "fdCode", inInfo.getBlock("result").getCell(0, "fdSuffix" ));
		}
		
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validateValue = validate(inInfo, "ZMCEEquipmentCard.validate", params);
		if(validateValue) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCEEquipmentCard.update");
			return outInfo;
		}
	}

	/**
	 * 设备删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEEquipmentCard</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:24:03 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEEquipmentCard.delete");
		super.delete(inInfo, "ZMCEAffiliatedEquipment.deleteByEquipment");
		super.delete(inInfo, "ZMCEAccessories.deleteByEquipment");
		super.delete(inInfo, "ZMCETools.deleteByEquipment");
		super.delete(inInfo, "ZMCEEquipmentInformation.deleteByEquipment");
		super.delete(inInfo, "ZMCEMaintenanceStandard.deleteByEquipment");
		super.delete(inInfo, "ZMCELubricationStandard.deleteByEquipment");
		super.delete(inInfo, "ZMCERepairStandard.deleteByEquipment");
		super.delete(inInfo, "ZMCEPointStandard.deleteByEquipment");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}

	/**
	 * 生效
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo effect(EiInfo inInfo)
	{
		for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
		{
			String standardId = StandardEquipment.insertStandard(inInfo.getBlock("result").getCellStr(0, "fdName"), 
					inInfo.getBlock("result").getCellStr(0, "fdModel"), 
					inInfo.getBlock("result").getCellStr(0, "fdSpecifications"),
					inInfo.getBlock("result").getCellStr(0, "fdEquipmentClassId"),
					inInfo.getBlock("result").getCellStr(0, "fdToencode"));
			inInfo.getBlock("result").setCell(i, "fdStandredEquipmentId", standardId);
		}	
		EiInfo _temp = super.update(inInfo, "ZMCEEquipmentCard.effect");		
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}		
	
	/**
	 * 引入标准
	 * <p>Function: addStandard</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午01:19:54 张志芳
	 * </p>
	 *
	 */
	public EiInfo addStandard(EiInfo inInfo)
	{		
		String standardId = StandardEquipment.containsStandard(inInfo.getBlock("result").getCellStr(0, "fdName"), 
				inInfo.getBlock("result").getCellStr(0, "fdModel"), 
				inInfo.getBlock("result").getCellStr(0, "fdSpecifications"),
				inInfo.getBlock("result").getCellStr(0, "fdEquipmentClassId"));
		if("".equals(standardId))
		{
			inInfo.setStatus(-1);
			return inInfo;
		}
		String keyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		//StandardEquipment.clearAllStandard(keyId);//删除原来的标准
		
		InsertAllStandard(keyId, standardId, "1");			
		
        EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 存为标准
	 * <p>Function: saveStandard</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-13下午01:20:17 张志芳
	 * </p>
	 *
	 */
	public EiInfo saveStandard(EiInfo inInfo)
	{
		String keyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		String standardId = StandardEquipment.containsStandard(inInfo.getBlock("result").getCellStr(0, "fdName"), 
				inInfo.getBlock("result").getCellStr(0, "fdModel"), 
				inInfo.getBlock("result").getCellStr(0, "fdSpecifications"),
				inInfo.getBlock("result").getCellStr(0, "fdEquipmentClassId"));
		if("".equals(standardId))
		{
			standardId = TablePk.getInstance().getNextValue("T_CE_STANDREDEQUIPMENT");
			inInfo.getBlock("result").setCell(0, "fdKeyId", standardId);//标准设备主键
			super.insert(inInfo, "ZMCEStandredEquipment.insert");	
		}
//		else
//		{
//			StandardEquipment.clearAllStandard(standardId);//删除原来的标准
//		}			
		
		InsertAllStandard(standardId, keyId, "0");
		
        EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 引入其它标准设备的标准
	 * <p>Function: addOtherStandard</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-18下午03:04:08 张志芳
	 * </p>
	 *
	 */
	public EiInfo addOtherStandard(EiInfo inInfo)
	{
		//StandardEquipment.clearAllStandard(inInfo.getString("cordKeyId"));//删除原来的标准
		InsertAllStandard(inInfo.getString("cordKeyId"), inInfo.getString("standardKeyId"), "1");
		return inInfo;
	}
	
	/**
	 * 将根据currentId设备ID查询到所有的标准，复制到ID为originalId的设备下面对应的各个标准。
	 * form取值为0和1，0代表标准设备，1代表设备卡片
	 * <p>Function: InsertAllStandard</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-18下午02:54:04 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo InsertAllStandard(String currentId, String originalId, String form)
	{		
		EiInfo info = new EiInfo();
		
		Map row = new HashMap();
		row.put("fdStandardsForm", form.equals("0")?"1":"0");
		row.put("fdEquipmentId", originalId);
		info.addBlock(EiConstant.queryBlock).addRow(row);
		info.addBlock("result").set("limit", "100");
		//保养标准
		info = super.query(info, "ZMCEMaintenanceStandard.query", new ZMCEMaintenanceStandard(), false, null, EiConstant.queryBlock, "result", "result");
		
		for(int i=0; i<info.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_MAINTENANCESTANDARD"));
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdEquipmentId", currentId);
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdStandardsForm", form);
		}
		super.insert(info, "ZMCEMaintenanceStandard.insert");
		
		//润滑标准
		info = super.query(info, "ZMCELubricationStandard.query", new ZMCELubricationStandard(), false, null, EiConstant.queryBlock, "result", "result"); 
		
		for(int i=0; i<info.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_LUBRICATIONSTANDARD"));
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdEquipmentId", currentId);
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdStandardsForm", form);
		}
		super.insert(info, "ZMCELubricationStandard.insert");
		
		//维修标准
		info = super.query(info, "ZMCERepairStandard.query", new ZMCERepairStandard(), false, null, EiConstant.queryBlock, "result", "result");
		
		for(int i=0; i<info.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_REPAIRSTANDARD"));
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdEquipmentId", currentId);
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdStandardsForm", form);
		}
		super.insert(info, "ZMCERepairStandard.insert");
		
		//点检标准
		info = super.query(info, "ZMCEPointStandard.query", new ZMCEPointStandard(), false, null, EiConstant.queryBlock, "result", "result");
		
		for(int i=0; i<info.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_POINTSTANDARD"));
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdStandredequipmentId", currentId);
			info.getBlock(EiConstant.resultBlock).setCell(i, "fdStandardsForm", form);
		}
		super.insert(info, "ZMCEPointStandard.insert");
		
		return info;
	}	

	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-26下午04:26:16 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {		
		if(params == null) {
			params = new HashMap<String, String>();
		}
		params.put("fdCode", inInfo.getBlock("result").getCellStr(0, "fdCode"));
		if("".equals(inInfo.getBlock("result").getCellStr(0, "fdCode")))//编号为空时不需要校验
		{
			return false;
		}
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("编号不能重复！");
			return true;
		}
		return false;
	}
	
	/**
	 * 内码拼接
	 * <p>Function: ConnectStr</p>
	 * <p>Description: </p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-15下午02:38:16 张志芳
	 * </p>
	 *
	 */
	 public String ConnectStr(String str1, String str2){
		StringBuffer fdToencode=new StringBuffer();
		fdToencode.append(str1).append(str2);
		return fdToencode.toString();
		  
	 }
}
